package com.distribute.framework.core.lsf;

import com.distribute.framework.core.web.helper.Utils;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;

public class LsfServiceProxy implements InvocationHandler,FactoryBean,InitializingBean {
	
	String serviceInterface;
	private Object serviceProxy;
	private Lsf lsf;
    
	
	public void afterPropertiesSet() throws Exception {
		this.serviceProxy = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{Class.forName(serviceInterface)}, this);
	}

	public Object invoke(Object object, Method method, Object[] args)throws Throwable {		
		try{
			return lsf.getCustomer().invoker(serviceInterface, method.getName(), args);
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex.getCause();
		}
	}
	public String getServiceInterface() {
		return serviceInterface;
	}

	public void setServiceInterface(String serviceInterface) {
		this.serviceInterface = serviceInterface;
	}

	public Object getObject() throws Exception {
		return this.serviceProxy;
	}

	public Class getObjectType() {
		try {
			if(!Utils.empty(serviceInterface))
				return Class.forName(serviceInterface);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	public boolean isSingleton() {
		return false;
	}

	public Lsf getLsf() {
		return lsf;
	}

	public void setLsf(Lsf lsf) {
		this.lsf = lsf;
	}


	

}